package com.xxy.crm.controller;

import com.xxy.crm.annoation.RequiredPermission;
import com.xxy.crm.base.BaseController;
import com.xxy.crm.base.ResultInfo;
import com.xxy.crm.dao.SaleChanceMapper;
import com.xxy.crm.enums.StateStatus;
import com.xxy.crm.query.SaleChanceQuery;
import com.xxy.crm.service.SaleChanceService;
import com.xxy.crm.utils.CookieUtil;
import com.xxy.crm.utils.LoginUserUtil;
import com.xxy.crm.vo.SaleChance;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.http.HTTPBinding;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/sale_chance")
public class SaleChanceController extends BaseController {
    @Resource
    private SaleChanceService saleChanceService;

    /**
     * 营销机会数据查询（分页多条件查询） 101001
     * 如果flag的值不为空，且值为1，则表示当前查询的是客户开发计划；否则查询营销机会数据
     * @param saleChanceQuery
     * @return
     */
    @RequiredPermission(code = "101001")
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> querySaleChanceByParams(SaleChanceQuery saleChanceQuery,Integer flag,HttpServletRequest request){
        //判断flag的值
        if (flag != null && flag ==1){
            // 查询客户开发计划
            // 设置分配状态
            saleChanceQuery.setState(StateStatus.STATED.getType());
            // 设置指派人（当前登录用户的ID）
            // 从cookie中获取当前登录用户的ID
            Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
            saleChanceQuery.setAssignMan(String.valueOf(userId));
        }
        return saleChanceService.querySaleChanceByParams(saleChanceQuery);
    }
    /**
     * 进⼊营销机会⻚⾯
     * @return
     */
    @RequiredPermission(code = "1010")
    @RequestMapping("/index")
    public String index(){
        return "saleChance/sale_chance";
    }

    /**
     * 添加营销机会
     *
     * @param saleChance
     * @return com.xxy.crm.base.ResultInfo
     */
    @RequiredPermission(code = "101002")
    @PostMapping("/add")
    @ResponseBody
    public ResultInfo addSaleChance(SaleChance saleChance, HttpServletRequest request){
        //从cookie中获取当前登录的用户名
        String userName = CookieUtil.getCookieValue(request,"userName");
        //设置用户名到营销机会对象
        saleChance.setCreateMan(userName);
        //调用service层的添加方法
        saleChanceService.addSaleChance(saleChance);
        return success("营销机会添加成功"); //resultInfo中的success方法
    }

    /**
     * 修改营销机会
     * @param saleChance
     * @param request
     * @return
     */
    @RequiredPermission(code = "101004")
    @PostMapping("/update")
    @ResponseBody
    public ResultInfo updateSaleChance(SaleChance saleChance, HttpServletRequest request){
        //调用service层的添加方法
        saleChanceService.updateSaleChance(saleChance);
        return success("营销机会更新成功"); //resultInfo中的success方法
    }


    /**
     * 进入添加/修改营销机会数据页面
     * @return
     */
    @RequestMapping("/toSaleChancePage")
    public String toSaleChancePage(Integer saleChanceId, HttpServletRequest request){

        //判断saleChanceId是否为空
        if (saleChanceId!=null){
            //通过id查询营销机会数据
            SaleChance saleChance = saleChanceService.selectByPrimaryKey(saleChanceId);
            request.setAttribute("saleChance",saleChance);
        }
        return "saleChance/add_update";
    }

    /**
     * 营销机会删除
     * @param ids
     * @return
     */
    @RequiredPermission(code = "101003")
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteSaleChance(Integer[] ids){
        //调用Service层的删除方法
        saleChanceService.deleteBatch(ids);
        return success("营销机会删除成功");
    }

    /**
     * 更新营销机会的开发状态
     * @param id
     * @param devResult
     * @return
     */
    @RequestMapping("/updateSaleChanceDevResult")
    @ResponseBody
    public ResultInfo updateSaleChanceDevResult(Integer id,Integer devResult){
        saleChanceService.updateSaleChanceDevResult(id,devResult);
        return success("开发状态修改成功");
    }
}

